package com.xindaoapp.happypet.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.bean.TuWen;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * WebView 查看商品图文详情老版本的
 * 
 * @author Yangqing
 * 
 */
public class TuWenActivity extends UMBaseActivity implements OnClickListener {
    private String id;
    @ViewInject(R.id.tittle_shop_cancel)
    private ImageView tittle_shop_cancel;
    @ViewInject(R.id.tittle_shop_content)
    private TextView tittle_shop_content;

    @ViewInject(R.id.ll)
    private LinearLayout ll;

    @ViewInject(R.id.no_result)
    private RelativeLayout no_result;


    @ViewInject(R.id.bottom_left)
    private ImageView bottom_left;
    @ViewInject(R.id.bottom_right)
    private ImageView bottom_right;
    @ViewInject(R.id.bottom_refresh)
    private ImageView bottom_refresh;

    @ViewInject(R.id.webview)
    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tuwen);
        ViewUtils.inject(this);
        id = getIntent().getStringExtra("id");
        initView();
    }

    private void initView() {
        bottom_left.setOnClickListener(this);
        bottom_right.setOnClickListener(this);
        bottom_refresh.setOnClickListener(this);
        tittle_shop_cancel.setOnClickListener(this);
        tittle_shop_content.setText("图文详情");
        initData(id);
    }

    private void initData(String id2) {
        RequestParams params = new RequestParams();
        params.put("goodsID", id2);
        final ProgressHUD progressHUD =
                ProgressHUD.show(TuWenActivity.this, "正在加载...", true, true, null);
        AddClient.get("http://ts.leepet.com/plugins/API.v1.0/?&a=goods&m=goodsInfo", params,
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (null != progressHUD && progressHUD.isShowing()) {
                            progressHUD.dismiss();
                        }
                        TuWen parse = null;
                        try {
                            parse = JSON.parseObject(content, TuWen.class);
                            if (!TextUtils.isEmpty(parse.data.goods_desc)) {
                                no_result.setVisibility(View.GONE);
                                webview.getSettings().setJavaScriptEnabled(true);
                                webview.getSettings().setDefaultTextEncodingName("UTF-8");
                                webview.getSettings().setSupportZoom(true);
                                // 缩放按钮
                                webview.getSettings().setBuiltInZoomControls(true);
                                webview.getSettings().setUseWideViewPort(true);
                                webview.getSettings().setLoadWithOverviewMode(true);
                                webview.loadDataWithBaseURL(null, parse.data.goods_desc,
                                        "text/html", "utf-8", null);
                                loadWebView();
                            } else {
                                no_result.setVisibility(View.VISIBLE);
                                ll.setVisibility(View.GONE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        super.onSuccess(statusCode, content);
                    }

                    @Override
                    public void onFailure(Throwable error, String content) {
                        if (null != progressHUD && progressHUD.isShowing()) {
                            progressHUD.dismiss();
                        }
                        super.onFailure(error, content);
                    }
                });
    }

    private void loadWebView() {
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null) {
                    webview.loadUrl(url);
                }
                return true;
            };
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_shop_cancel:
                finish();
                break;
            case R.id.bottom_left:
                if (webview.canGoBack()) {
                    webview.goBack();
                } else {
                    Toast.makeText(getApplicationContext(), "没有上一页", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bottom_right:
                if (webview.canGoForward()) {
                    webview.canGoForward();
                } else {
                    Toast.makeText(getApplicationContext(), "没有下一页", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bottom_refresh:
                loadWebView();
                webview.reload();
                break;
            default:
                break;
        }
    }
}
